package com.example.shellcollector.ShowApp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.shellcollector.DashboardFragment;
import com.example.shellcollector.Entity.InstalledAppUsage;
import com.example.shellcollector.HomeFragment;
import com.example.shellcollector.R;
import com.example.shellcollector.SettingsFragment;
import com.example.shellcollector.database.InstalledAppUsageDao;
import com.example.shellcollector.database.ShellCollectorDatabase;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class ShowActivity extends AppCompatActivity {
    private static final String TAG = "ShowActivityEntity";
    private InstalledAppUsageDao installedAppUsageDao;
    private RecyclerView recyclerView ;
    private List<InstalledAppUsage> installedAppUsages;
    private MyAdapterforshow myAdapterforshow;
    private final MaterialToolbar.OnMenuItemClickListener onMenuItemClickListener
            = item -> {
        switch (item.getItemId()) {
            case R.id.byname:
                recyclerView.removeAllViews();
                installedAppUsages = installedAppUsageDao.getAllOrderByAppName(false);
                myAdapterforshow = new MyAdapterforshow(installedAppUsages,getPackageManager());
                myAdapterforshow.setOnItemClickListener(new MyAdapterforshow.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(ShowActivity.this,ShowAppDetails.class);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("InstalledAppUsage", installedAppUsages.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(myAdapterforshow);
                return true;
            case R.id.bytotaltime:
                recyclerView.removeAllViews();
                installedAppUsages = installedAppUsageDao.getAllOrderByTotalRunningTime(false);
                myAdapterforshow= new MyAdapterforshow(installedAppUsages,getPackageManager());
                myAdapterforshow.setOnItemClickListener(new MyAdapterforshow.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(ShowActivity.this,ShowAppDetails.class);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("InstalledAppUsage", installedAppUsages.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(myAdapterforshow);
                return true;
            case R.id.byfirsttime:
                recyclerView.removeAllViews();
                installedAppUsages = installedAppUsageDao.getAllOrderByAppName(true);
                myAdapterforshow = new MyAdapterforshow(installedAppUsages,getPackageManager());
                myAdapterforshow.setOnItemClickListener(new MyAdapterforshow.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(ShowActivity.this,ShowAppDetails.class);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("InstalledAppUsage", installedAppUsages.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(myAdapterforshow);
                return true;
            case R.id.bylasttime:
                recyclerView.removeAllViews();
                installedAppUsages = installedAppUsageDao.getAllOrderByLastRunningTime(false);
                myAdapterforshow = new MyAdapterforshow(installedAppUsages,getPackageManager());
                myAdapterforshow.setOnItemClickListener(new MyAdapterforshow.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(ShowActivity.this,ShowAppDetails.class);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("InstalledAppUsage", installedAppUsages.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(myAdapterforshow);
                return true;
            case R.id.bylaunchtime:
                recyclerView.removeAllViews();
                installedAppUsages = installedAppUsageDao.getAllOrderByLaunchCount(false);
                myAdapterforshow = new MyAdapterforshow(installedAppUsages,getPackageManager());
                myAdapterforshow.setOnItemClickListener(new MyAdapterforshow.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent(ShowActivity.this,ShowAppDetails.class);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("InstalledAppUsage", installedAppUsages.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(myAdapterforshow);
                return true;
        }
        return false;
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        MaterialToolbar topAppbar = findViewById(R.id.materialToolbar);
        topAppbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        topAppbar.setOnMenuItemClickListener(onMenuItemClickListener);
        installedAppUsageDao = ShellCollectorDatabase.getDatabase(getApplication()).getInstalledAppUsageDao();
        ArrayList<HashMap<String,Object>> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(2020, 6, 8);
        long first = calendar.getTimeInMillis();
        calendar.set(2020, 9, 17);
        long last = calendar.getTimeInMillis();
        long total = 360 * 60 * 1000;
        installedAppUsages = getInstalledAppUsageFromDB();
        recyclerView = findViewById(R.id.showapprecyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        MyAdapterforshow myAdapterforshow = new MyAdapterforshow(installedAppUsages,getPackageManager());
        myAdapterforshow.setOnItemClickListener(new MyAdapterforshow.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(ShowActivity.this,ShowAppDetails.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("InstalledAppUsage", installedAppUsages.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(myAdapterforshow);

    }

    private List<InstalledAppUsage> getInstalledAppUsageFromDB() {
        return installedAppUsageDao.getAllInstalledAppUsages();
    }

}